package android.pot.com.smartcar.tool;

import android.content.Context;
import android.pot.com.smartcar.config.MyConfig;
import android.pot.com.smartcar.entity.UserInfo;
import android.pot.com.smartcar.util.SaveObjectUtils;
import android.text.TextUtils;



/**
 * Created by zhaoshilu on 2017/8/24.
 */

public class LoginTool {

    /**
     *  开始初始化UserInfo数据
     */
    public static void initUserInfo(Context context){
        SaveObjectUtils saveObjectUtils = new SaveObjectUtils(context, MyConfig.USERINFO_KEY);
        UserInfo userInfo = saveObjectUtils.getObject(MyConfig.USERINFO_KEY, UserInfo.class);
        UserInfo info =  UserInfo.instance() ;
        if (null != userInfo){
            info.setToken(userInfo.getToken());
            info.setId(userInfo.getId());
            info.setPhone(userInfo.getPhone());
            info.setLast_login_time(userInfo.getLast_login_time());
            info.setDistribution_status(userInfo.getDistribution_status());

        }else{
            info.setId("");
//            info.setPhone(userInfo.getPhone());
            info.setLast_login_time("");
            info.setToken("");
            info.setDistribution_status(0);
        }

    }



    /**
     *  登录后重新赋值UserInfo,保存UserInfo数据
     */
    public static void setLoginIn(Context context,UserInfo userInfo){
        UserInfo info =  UserInfo.instance() ;
        info.setId(userInfo.getId());
        info.setPhone(userInfo.getPhone());
        info.setLast_login_time(userInfo.getLast_login_time());
        info.setToken(userInfo.getToken());
        info.setDistribution_status(userInfo.getDistribution_status());
        SaveObjectUtils utils = new SaveObjectUtils(context, MyConfig.USERINFO_KEY);
        utils.setObject(MyConfig.USERINFO_KEY,info);
    }


    /**
     *  获取ssid
     */
    public static String getToken(){

        UserInfo userInfo = UserInfo.instance();
            if (!TextUtils.isEmpty(userInfo.getToken())){
                return userInfo.getToken();
            }else{
                return "";
            }
    }

    /**
     * 退出登录清空UserInfo数据，shareperence数据
     */
    public static void setLoginOut(Context context){
        SaveObjectUtils utils = new SaveObjectUtils(context, MyConfig.USERINFO_KEY);
        utils.setObject(MyConfig.USERINFO_KEY,new UserInfo());
        UserInfo userInfo = UserInfo.instance();
        userInfo.setId("");
//            info.setPhone(userInfo.getPhone());
        userInfo.setLast_login_time("");
        userInfo.setToken("");
        userInfo.setDistribution_status(0);

    }
}
